package edu.nf.project.services.accountsAndPermissions.impl;

import edu.nf.project.entitys.dto.accountsAndPermissions.UserRoleDTO;
import edu.nf.project.entitys.entity.accountsAndPermissions.convention.Roles;
import edu.nf.project.entitys.entity.accountsAndPermissions.relevancy.Users_Roles;
import edu.nf.project.mappers.accountsAndPermissions.convention.RolesMapper;
import edu.nf.project.mappers.accountsAndPermissions.relevancy.Users_RolesMapper;
import edu.nf.project.services.accountsAndPermissions.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色 Service 实现类
 */
@Service
public class RolesServiceImpl implements RolesService {
    @Autowired
    private RolesMapper rolesMapper;

    @Autowired
    private Users_RolesMapper users_rolesMapper;

    @Override
    public List<Roles> getAllRoles() {
        return rolesMapper.selectList(null);
    }

    @Override
    @Transactional
    public boolean addRoles(Roles roles) {
        int result = rolesMapper.insert(roles);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean delRoles(Integer rId) {
        int result = rolesMapper.deleteById(rId);
        return result > 0;
    }

    @Override
    public boolean updRoles(Roles roles) {
        int result = rolesMapper.updateById(roles);
        return result > 0;
    }

    @Override
    public List<UserRoleDTO> getAllUserRoles() {
        return rolesMapper.selectAllUserRoles();
    }

    @Override
    public boolean rolesForUser(Integer uId, Integer rId) {
        Users_Roles ur = new Users_Roles();
        ur.setUId(uId);
        ur.setRId(rId);
        return users_rolesMapper.insert(ur) > 0;
    }

    @Override
    public boolean delUsersRoles(Integer uId, Integer rId) {
        Users_Roles ur = new Users_Roles();
        ur.setUId(uId);
        ur.setRId(rId);
        return users_rolesMapper.deleteById(ur) > 0;
    }
}
